using Bdev.Net.Dns;
using Bdev.Net.Dns.Records;
using System;
using System.Net;
using System.Diagnostics;


namespace TestApp 
{
	static class Program
	{
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
			string query = args[0];
            
			Request req = new Request();
            
			DnsClass qclass = DnsClass.IN;
            
			DnsQType qtype = DnsQType.IPSECKEY;
            
			IPAddress dnsServer = IPAddress.Parse("8.8.8.8");
            
			Question q = new Question(query, qtype, qclass);
			
            req.AddQuestion(q);
			
            Stopwatch clock = new Stopwatch();
            clock.Start();
			Response r = Resolver.Lookup(req, dnsServer);
			clock.Stop();
			
            // check for a response
            if (r == null)
            {
                Console.WriteLine("No answer");
                return;
            }
			
			
            Console.WriteLine(string.Format(";; ->>HEADER<<- opcode: {0}, status: {1}, id: {2}",
                                              req.Opcode.ToString(),
                                              r.ReturnCode.ToString(),
                                              r.ID.ToString()));

            Console.WriteLine(string.Format(";; flags:{0}{1}{2}{3}; QUERY {4}, ANSWER: {5}, AUTHORITY: {6}, ADDITIONAL: {7}\r\n",
                                              !r.QR ? " qr" : "",                   // query/response
                                              r.AuthoritativeAnswer ? " aa" : "",   // authoritative answer
                                              req.RecursionDesired ? " rd" : "",    // recursion desired
                                              r.RecursionAvailable ? " ra" : "",    // recursion available
                                              r.Questions.Length,
                                              r.Answers.Length,
                                              r.NameServers.Length,
                                              r.AdditionalRecords.Length));
            if (r.Questions.Length > 0)
            {
                Console.WriteLine(";; QUESTION SECTION:");
                foreach (Question rq in r.Questions)
                {
                    Console.WriteLine(String.Format(";{0}\t\t\t{1}\t{2}",
                                                      rq.Domain,
                                                      rq.Class.ToString(),
                                                      rq.QType.ToString())
                                        );
                }
                Console.WriteLine("");
            }

            if (r.Answers.Length > 0)
            {
                Console.WriteLine(";; ANSWER SECTION:");
                foreach (Answer ra in r.Answers)
                {
                    Console.WriteLine(String.Format("{0,-32}{1}\t{2}\t{3}\t{4}",
                                                      ra.Domain,
                                                      ra.Ttl.ToString(),
                                                      ra.Class.ToString(),
                                                      ra.Type.ToString(),
                                                      ra.Record.ToString())
                                        );
                }
                Console.WriteLine("");
            }

            if (r.NameServers.Length > 0)
            {
                Console.WriteLine(";; AUTHORITY SECTION:");
                foreach (NameServer rn in r.NameServers)
                {
                    Console.WriteLine(String.Format("{0,-32}{1}\t{2}\t{3}\t{4}",
                                                      rn.Domain,
                                                      rn.Ttl.ToString(),
                                                      rn.Class.ToString(),
                                                      rn.Type.ToString(),
                                                      rn.Record.ToString())
                                        );
                }
                Console.WriteLine("");
            }

            if (r.AdditionalRecords.Length > 0)
            {
                Console.WriteLine(";; ADDITIONAL SECTION:");
                foreach (AdditionalRecord ra in r.AdditionalRecords)
                {
                    Console.WriteLine(String.Format("{0,-32}{1}\t{2}\t{3}\t{4}",
                                                      ra.Domain,
                                                      ra.Ttl.ToString(),
                                                      ra.Class.ToString(),
                                                      ra.Type.ToString(),
                                                      ra.Record.ToString())
                                        );
                }
                Console.WriteLine("");
            }

            Console.WriteLine(String.Format(";; Query time: {0} msec", clock.ElapsedMilliseconds));
            Console.WriteLine(String.Format(";; SERVER: {0}#{1}({2})", dnsServer.ToString(), 53, dnsServer.ToString()));
            Console.WriteLine(String.Format(";; WHEN: {0}", DateTime.Now.ToString("ddd MMM dd HH:mm:ss yyyy")));
            //TODO: MessageSize is not giving correct info...
            Console.WriteLine(String.Format(";; MSG SIZE  rcvd: {0}", r.MessageSize));
        }
	}	
}